lobsterlayui.main(['index', 'layer', 'form', 'element', 'table','tree'], {
    data: {
        $layui: null,
        tableins: null,
        selectNode: { cid: '', pid: '', lv: 1, onlyValue: false }
    },
    pageload: function (right) {
        console.log(right);
        this.initevent();
        this.treeLoad();
    },
    initevent() {
        var self = this;
        layui.form.on('checkbox(onlyValuefilter)', function (data) {
            self.data.selectNode.onlyValue = data.elem.checked;
            //console.log(selectNode.onlyValue);
        });

        layui.form.on("submit(LAY-app-front-search)", function (data) {
            self.data.tableins.reload({
                where: {
                    cid: self.data.selectNode.cid,
                    pid: self.data.selectNode.pid,
                    lv: self.data.selectNode.lv,
                    param: $("#param").val(),
                    onlyValue: self.data.selectNode.onlyValue
                },
                page: {
                    curr: 1
                }
            });
        });

        layui.table.on('tool(entlist)', function (obj) {
            var data = obj.data; //当前行数据
            switch (obj.event) {
                case 'detail':
                    var index = layer.open({
                        type: 1,
                        title: "查看值域",
                        btn: ["关闭"],
                        btnAlign: "r",
                        content: $('#dialog').html(),
                        area: ["60%", "90%"],
                        success: function (layero, index) {
                            self.rangetableLoad(data['值域编码']);
                        },
                        yes: function (index, layero) {
                            layer.close(index);
                        }
                    });
                    break;
            }
        });
    },

    //获取树结构
    treeLoad() {
        var self = this;
        lobsterlayui.GETAPI('tsgbdata.gettree').then(retdata => {
            var treelist = retdata;
            self.viewToTree(treelist);
        })
    },

    //树显示
    viewToTree(treelist) {
        var self = this;
        layui.tree.render({
            elem: '#Left',
            isJump: true,
            onlyIconControl: true,
            autoRefresh: true,
            customOperate: true,
            data: treelist,
            edit: [], //操作节点的图标
            operate: function (obj) {
                var type = obj.type; //得到操作类型：
                var data = obj.data; //得到当前节点的数据
                var elem = obj.elem; //得到当前节点元素
                var id = data.id;
                var name = data.title;
            },
            click: function (obj) {
                var data = obj.data;

                $("#Left .nodeover").removeClass("nodeover");
                $(obj.elem).find(".layui-tree-main").eq(0).addClass("nodeover");
                self.data.selectNode.cid = data.id;
                self.data.selectNode.pid = data.parentid;
                self.data.selectNode.lv = data.level;
                self.TableLoad();
            }
        });
    },

    //类型列表加载
    TableLoad() {
        var self = this;
        if (self.data.selectNode.lv != 2 && self.data.selectNode.lv != 3) {
            return;
        }
        var paramstr = $("#param").val();
        self.data.tableins = layui.table.render({
            elem: "#entlist",
            headers: lobsterlayui.GetHeaderData(),
            where: {
                cid: self.data.selectNode.cid,
                pid: self.data.selectNode.pid,
                lv: self.data.selectNode.lv,
                param: paramstr,
                onlyValue: self.data.selectNode.onlyValue
            },
            url: lobsterlayui.GetLowCodeUrl('tsgbdata.getmetadata'),
            defaultToolbar: [],
            //height: "full-70",
            size: '10',
            page: true, //开启分页
            //even: true,
            loading: true,
            cols: [
                [
                    { field: '内部标识符', title: '内部标识符', width: '10%' },
                    { field: '数据集编码', title: '数据集编码', width: '10%' },
                    { field: '数据元标识符', title: '数据元标识符', width: '10%' },
                    { field: '数据元名称', title: '数据元名称', width: '20%' },
                    { field: '定义', title: '定义', width: '20%' },
                    { field: '类型', title: '类型', width: '7%' },
                    { field: '格式', title: '格式', width: '7%' },
                    { field: '数据元允许值', title: '数据元允许值', width: '20%' },
                    { title: '操作', toolbar: '#classoper', width: '7%', fixed: 'right' }
                ]
            ],
            done: function (res) {
                var viewList = res.data;
            },
            parseData: function (res) {
                return {
                    code: res.code,
                    msg: res.msg,
                    count: res.data.count,
                    data: res.data.list
                }
            }
        });
    },

    rangetableLoad(rangeId) {
        var self=this;
        var arr = [ //表头
            { width: "10%", field: '值', title: '值' },
            { width: "20%", field: "值含义", title: "值含义" },
            { width: "30%", field: "说明", title: "说明" },
            { width: "20%", field: "值域编码", title: "值域编码" },
            { width: "20%", field: "值域名称", title: "值域名称" },
        ];
        layui.table.render({
            elem: "#rangetable",
            headers: lobsterlayui.GetHeaderData(),
            where: {
                cid: self.data.selectNode.cid,
                pid: self.data.selectNode.pid,
                lv: self.data.selectNode.lv,
                rangeId: rangeId
            },
            height: "full",
            cellMinWidth: 80, //全局定义常规单元格的最小宽度
            url: lobsterlayui.GetLowCodeUrl('tsgbdata.getrangedata'),
            page: true, //开启分页
            loading: true,
            cols: [arr],
            parseData: function (res) {
                return {
                    code: res.code,
                    msg: res.msg,
                    count: res.data.count,
                    data: res.data.list
                }
            }
        });
    }

})